const Koa = require('koa');// 创建一个Koa对象表示web app本身:const app = new Koa();// 注意require('koa-router')返回的是函数:const router = require('koa-router')();const koaBody = require('koa-body');
const fs = require('fs');
const path = require('path');app.use(koaBody({    multipart: true,    formidable: {        maxFileSize: 200*1024*1024    // 设置上传文件大小最大限制，默认2M    }}));

const bodyParser = require('koa-bodyparser');
app.use(bodyParser());
// add router middleware:app.use(router.routes());

const User = require('./db/User');
const Published = require('./db/Published');
const Retroaction = require('./db/Retroaction');
const staticFiles = require('koa-static')app.use(staticFiles(path.join(__dirname + '/public/')))// add url-route:router.get('/hello', async (ctx, next) => {    var name = ctx.request.query.name;	
	let datas = await User.findAll();
		
		ctx.body = datas});

router.get('/hi', async (ctx, next) => {    var name = ctx.request.query.name;	
	let datas = await Retroaction.findAll();
		
		ctx.body = datas});

router.post('/hello/add', async (ctx, next) => {    // var name = ctx.request.query.name;
	console.log('ctx.request.body',ctx.request.body)
	// let datas = await User.findAll();	
		
	const jane = await User.create(ctx.request.body);
	console.log("Jane's auto-generated ID:", jane.id);
	ctx.body = jane});

router.post('/hi/feedback', async(ctx, next) => {

	console.log('ctx.request.body',ctx.request.body)
	
	const jane = await Retroaction.create(ctx.request.body);
	console.log("Jane's auto-generated ID:", jane.id);
	ctx.body = jane
});

router.get('/hello/add', async(ctx, next) => {
    console.log("Published", Published)
    let publisheds = await Published.findAll();
    // console.log("publisheds", publisheds)
    // let obj = JSON.parse(publisheds)
    ctx.body = publisheds
});

router.post('/published', async(ctx, next) => {
    console.log("ctx.request.body", ctx.request.body)
        image = [];
        for (let i of ctx.request.body.imageList) {
            image.push(i)
        }
        console.log(image)
    ctx.request.body.imageList = JSON.stringify(ctx.request.body)
    const published = await Published.create(ctx.request.body);
    console.log("published's auto-generated ID:", published.id);
    ctx.body = published
});

router.post('/uploadfile', async (ctx, next) => {  
	// 上传单个文件  
	const file = ctx.request.files.file; // 获取上传文件  
	// 创建可读流  
	const reader = fs.createReadStream(file.path);  
	let filePath = path.join(__dirname, 'public/upload/') + `${file.name}`;	console.log(filePath)  
	// 创建可写流  
	const upStream = fs.createWriteStream(filePath);  
	// 可读流通过管道写入可写流  
	reader.pipe(upStream);  
	return ctx.body = `http://localhost:3000/upload/${file.name}`;
});
router.post('/uploadfiles', async (ctx, next) => {  
	// 上传单个文件  
	const files = ctx.request.files; 
	console.log("file",files)// 获取上传文件  
	
	let filess = []
	for (const key in files) {
		let file = files[key]
		// 创建可读流
		const reader = fs.createReadStream(file.path);  
		let filePath = path.join(__dirname, 'public/upload/') + `${file.name}`;
		console.log(filePath)  
		// 创建可写流  
		const upStream = fs.createWriteStream(filePath);  
		// 可读流通过管道写入可写流  
		reader.pipe(upStream);  
		filess.push(`http://localhost:3000/upload/${file.name}`) 
	}
	return ctx.body = filess;
});

let port = 3000// 在端口3000监听:app.listen(port);console.log(`Local:   http://localhost:${port}/`);